<template>
	<view>
		<view class="main">
			<u-form :model="sms" ref="uForm">
					<u-form-item  label="新的手机号" prop="newMobile" label-position="top">
						<u-input v-model="sms.newMobile"  placeholder="请输入新绑定的手机号" :border="true"  maxlength="11"  />
					</u-form-item>
					<u-form-item label="验证码" prop="code" label-position="top">
						<u-input v-model="sms.code"  placeholder="请输入验证码"  maxlength="6"  />
						<view :class="status == 1? 'before-style': 'after-style'" @tap="getVerifyCode" >{{tips}}</view>
						<u-toast ref="uToast"></u-toast>
						<u-verification-code start-text="发送验证码" change-text="Xs" :seconds="seconds" @end="end" @start="start" ref="uCode" 
						@change="codeChange"></u-verification-code>
					</u-form-item>
			</u-form>
			<u-button @click="submit">确认修改</u-button>
		</view>
	</view>
</template>

<script>
	import {API} from "@/config/myApi.js"
	export default {
		data() {
			return {
				tips: '',
				seconds: 60,
				status: 1,
				newMobileStatus:false,
				sms:{
					code:"",
					newMobile:"",
					verificationEnums:"EDIT_MOBILE"
				},
				rules: {
					code:[
						{
							required: true, 
							message: '请输入验证码',
							trigger: ['change','blur'],
						}
					],
					newMobile:[
						{
								required: true, 
								message: '请输入手机号',
								trigger: ['change','blur'],
							},
							{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// this.$u.test.mobile()就是返回true或者false的
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
				}
			}
		},
		onReady() {
				this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
					console.log('验证通过');
				} else {
					console.log('验证失败');
				}
			});
			},
			codeChange(text) {
				this.tips = text;
			},
			getVerifyCode() {
				if(this.$refs.uCode.canGetCode) {
				// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						this.newMobileStatus = true;
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				this.status = 1
				// this.$u.toast('倒计时结束');
			},
			start() {
				this.status = 2
				// this.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style>
	.main{
		margin: 24rpx 30rpx;
		padding: 18rpx 18rpx 40rpx;
		background-color: #fff;
		border-radius: 12rpx;
		box-shadow: 0 0 4rpx 0 #ccc;
	}
	.before-style {
		background: #007AFF;
		width: 30%;
		color: #fff;
		text-align: center;
		border: none;
/* 		padding-top: 20rpx;
		padding-bottom: 20rpx; */
		border-radius: 10rpx;
	}
	 
	.after-style {
		background:#C0C4CC;
		width: 30%;
		color: #fff;
		text-align: center;
		border: none;
/* 		padding-top: 20rpx;
		padding-bottom: 20rpx; */
		border-radius: 10rpx;
	}
</style>
